package com.cheboohui.iqs.impl;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSONObject;
import com.cheboohui.iqs.common.basic.StringUtil;
import com.cheboohui.iqs.common.basic.TimeUtil;
import com.cheboohui.iqs.config.PrivateTool;
import com.cheboohui.iqs.controller.BaseController;
import com.cheboohui.iqs.deal.BackDeal;
import com.cheboohui.iqs.deal.CheckDeal;
import com.cheboohui.iqs.deal.CustomizeDeal;
import com.cheboohui.iqs.deal.FrontDeal;
import com.cheboohui.iqs.deal.PriceDeal;
import com.cheboohui.iqs.menu.ErrorNews;
import com.cheboohui.iqs.service.Service;
import com.cheboohui.iqs.spring.entity.Policy;
import com.cheboohui.iqs.spring.entity.price.request.CheckCar;
import com.cheboohui.iqs.spring.entity.price.request.ConfirmInsured;
import com.cheboohui.iqs.spring.entity.price.request.PayRequest;
import com.cheboohui.iqs.spring.entity.price.request.PolicyRequest;
import com.cheboohui.iqs.spring.entity.price.request.PriceRequest;
import com.cheboohui.iqs.spring.entity.price.request.PriviousRequest;
import com.cheboohui.iqs.spring.entity.price.response.CheckCarResult;
import com.cheboohui.iqs.spring.entity.price.response.ConfirmInsuredResult;
import com.cheboohui.iqs.spring.entity.price.response.PriceResult;
import com.cheboohui.iqs.spring.entity.price.response.PriviousResult;
import com.cheboohui.iqs.sql.entity.Pay;
import com.cheboohui.iqs.sql.entity.SqlResult;
import com.cheboohui.iqs.sql.service.PayService;
import com.cheboohui.iqs.taiping.back.BackYeGaunDeal;
import com.cheboohui.iqs.taiping.direct.order.PayPolicy;

@org.springframework.stereotype.Service
public class ServiceImpl implements Service {

	/**
	 * logger:日志工具.
	 */
	private static Log log = LogFactory.getLog(ServiceImpl.class);
	/****************************** 报价 *************************************/

	@Autowired
	PayService db;

	/**
	 * 车辆查询
	 * 
	 * @param checkCar
	 *            请求参数
	 * @return
	 */
	@Override
	public CheckCarResult checkCar(CheckCar checkCar) {
		return PriceDeal.checkCar(checkCar);
	}

	/**
	 * 车险保费计算
	 * 
	 * @param priceRequest
	 *            请求参数
	 * @return
	 */
	@Override
	public PriceResult price(PriceRequest priceRequest) {
		return PriceDeal.carPrice(priceRequest);
	}

	/**
	 * 确认投保
	 * 
	 * @param confirmInsured
	 *            请求参数
	 * @return
	 */
	@Override
	public ConfirmInsuredResult confirm(ConfirmInsured confirmInsured) {
		return PriceDeal.confirm(confirmInsured);
	}

	/**
	 * 支付登记
	 * 
	 * @param payRequest
	 *            请求参数
	 * @return
	 */
	@Override
	public ConfirmInsuredResult pay(PayRequest payRequest) {
		log.info(payRequest.getPolicy().getOrderNo() + "支付登记数据存入MYSQL...开始");
		SqlResult dbResult = db.payAdd(PayPolicy.getDB(payRequest));
		log.info(payRequest.getPolicy().getOrderNo() + "支付登记数据存入MYSQL...结果:" + JSONObject.toJSONString(dbResult));
		if (dbResult.isState())
			return PriceDeal.pay(payRequest);
		else
			return new ConfirmInsuredResult(ErrorNews.L03, "支付登记数据存入MYSQL出现错误:" + dbResult.getMsg());
	}

	/****************************** 辅助查询接口 *************************************/

	/**
	 * 往年投保单查询
	 * 
	 * @param priviousRequest
	 *            请求参数
	 * @return
	 */
	@Override
	public PriviousResult privious(PriviousRequest priviousRequest) {
		return CheckDeal.privious(priviousRequest);
	}

	/**
	 * 保单相关查询
	 * 
	 * @param policyRequest
	 *            保单相关信息
	 * @param type
	 *            类型：01,投保单查询;02,保单查询
	 * @return
	 */
	@Override
	public PriviousResult policy(PolicyRequest policyRequest, String type) {
		return CheckDeal.policy(policyRequest, type);
	}

	/**
	 * 根据据投保单号查询单子状态
	 * 
	 * @param policyRequest
	 * @return
	 */
	@Override
	public ConfirmInsuredResult checkOrder(PolicyRequest policyRequest) {
		return CheckDeal.checkOrder(policyRequest);
	}

	/****************************** 保险公司回调接口 *************************************/

	/**
	 * 
	 * back:(统一回调处理). <br/>
	 * 
	 * @param type
	 *            0统一回调;1支付回调;2承保回调;3配送回调
	 * @param request
	 * @param response
	 */
	@Override
	public void back(HttpServletRequest request, HttpServletResponse response, int type) {
		Pay pay = new Pay();

		if (1 == type || 2 == type) {
			JSONObject xml = BackYeGaunDeal.xmlJson(BackDeal.getXML(request));
			log.info("回调json报文:" + xml);
			if (null != xml && !xml.isEmpty()) {
				String orderNo = xml.getJSONObject("body").getString("orderNo");
				pay.setOut_trade_no(orderNo);
				log.info(orderNo + "支付登记数据查询MYSQL...开始");

				SqlResult dbResult = db.paySelect(pay);
				log.info("支付登记数据查询MYSQL...结果:" + JSONObject.toJSONString(dbResult));

				BackDeal.back(response, type, xml, dbResult);
			} else {
				log.info(xml.getJSONObject("body").getString("orderNo") + "回调xml报文格式错误....");
				BackDeal.back(response, type, null, null);
			}
		} else {
			BackDeal.back(request, response, type);
		}
	}

	/****************************** 页面 *************************************/

	/**
	 * 根据需求获取页面(前端回调页面统一处理)
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@Override
	public ModelAndView getFront(HttpServletRequest request, HttpServletResponse response, String type) {
		return FrontDeal.getFront(request, response, type);
	}

	/****************************** 页面 *************************************/

	/**
	 * 自定义接口
	 * 
	 * @param request
	 * @param response
	 */
	@Override
	public void customize(HttpServletRequest request, HttpServletResponse response) {
		try {
			JSONObject param = PrivateTool.paramtenToJson(request.getParameterMap());
			if(null==param||param.isEmpty()){
				BaseController.setResponseText(response, "请求数据为空");
			}
			
			if("mysql".equals(param.getString("type"))){
				String orderNo=param.getString("orderNo");
				if(!StringUtil.isEmpty(orderNo)){
					Pay pay = new Pay();
					pay.setSource("TAIPING");
					pay.setCompany_code("TAIPING");
					pay.setGrade_time(TimeUtil.timeFormat(new Date(), TimeUtil.DEFAULT_TIME));
					pay.setPay_process("paybegin");
					pay.setOut_trade_no(orderNo);// 订单号
					pay.setOreder_subject("太平直连支付");
					pay.setTotal_amount("" + 100);// 订单金额
					pay.setBody("太平直连---支付登记");
					pay.setTimeout_express("120");
					pay.setShow_url("www.bbbb.contract.com");// 支付后承保回调地址
					pay.setSuccess_back_url("www.bbbb.front.com");// 前端地址
					pay.setFail_back_url("www.bbbb.front.com");// 前端地址
					pay.setProduct_code("TP-DIRECT");
					pay.setFront_notice("www.bbbb.zhifu.com");// 支付回调地址
					SqlResult dbResult = db.payAdd(pay);
					log.info(orderNo + "测试支付登记数据存入MYSQL...结果:" + JSONObject.toJSONString(dbResult));
					if (dbResult.isState()){
						BaseController.setResponseText(response, JSONObject.toJSONString(dbResult));
						return ;
					}
				}
			}else if("select".equals(param.getString("type"))){
				Pay pay = new Pay();
				String orderNo=param.getString("orderNo");
				if(!StringUtil.isEmpty(orderNo)){
					pay.setOut_trade_no(orderNo);// 订单号
					SqlResult dbResult = db.paySelect(pay);
					log.info(orderNo + "测试支付登记数据存入MYSQL...结果:" + JSONObject.toJSONString(dbResult));
					BaseController.setResponseText(response, JSONObject.toJSONString(dbResult));
					return ;
				}
				
			}
			
			BaseController.setResponseText(response, "暂时该接口未启用");
		} catch (Exception e) {
			BaseController.setResponseText(response, e.getMessage());
		}
		
		//CustomizeDeal.customize(request, response);
	}
}
